
import os
import time
from TTS.api import TTS
from pydub import AudioSegment
from pydub.playback import play

# =====================
# 配置部分
# =====================
# 输出目录
output_dir = r"C:\Users\33720\Desktop\temp\tts"
os.makedirs(output_dir, exist_ok=True)

# 日语单词列表
words = [
    "鍋", "フライパン", "包丁", "まな板", "お玉",
    "しゃもじ", "おろし金", "湯呑み", "茶碗", "皿"
]

# =====================
# 初始化 TTS
# =====================
tts = TTS(model_name="tts_models/ja/kokoro/tacotron2-DDC", progress_bar=True, gpu=False)

# =====================
# 生成音频
# =====================
for i, word in enumerate(words, start=1):
    filename = os.path.join(output_dir, f"{i}_{word}.wav")
    print(f"正在生成: {filename}")

    # 生成语音文件
    tts.tts_to_file(text=word, file_path=filename)

    # 播放音频（可选）
    # audio = AudioSegment.from_wav(filename)
    # play(audio)

    # 单词间暂停 0.8 秒
    # time.sleep(0.8)

print("所有单词音频生成完成！")
